package com.qf.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qf.dao.TeacherMapper;
import com.qf.entity.TbTeacher;
import com.qf.service.TeacherService;
import com.qf.utils.QiNiuUpload;
import com.qf.utils.ResultMsg;
import com.qf.utils.SessionKey;
import com.qf.utils.TokenUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;

/**
 * @Author:LGD
 * @Date:2023/7/17 17:17
 */
@Service
public class TeacherServiceImpl extends ServiceImpl<TeacherMapper, TbTeacher> implements TeacherService {

    @Autowired
    QiNiuUpload qiNiuUpload;

    /**
     * 查询教师+根据教师姓名
     *
     * @param page        当前页
     * @param limit       每页显示条数
     * @param teacherName 教师姓名
     * @return
     */
    @Override
    public ResultMsg findAllTeacher(int page, int limit, String teacherName, String teacherTeach) {
        QueryWrapper<TbTeacher> wrapper = new QueryWrapper<>();
        Page<TbTeacher> teacherPage = new Page<>(page, limit);

//        if (teacherName==null && teacherTeach==null){
//        }
        //根据教师名搜索
        if (teacherName != null) {
            wrapper.like("teacher_name", teacherName);
            return ResultMsg.success(this.page(teacherPage, wrapper));
        } else if (teacherTeach != null) {
            //根据所教内容
            wrapper.eq("teacher_teach", teacherTeach);
            return ResultMsg.success(this.page(teacherPage, wrapper));
        }


        //        //都没有参数
        return ResultMsg.success(this.page(teacherPage));

    }

    @Override
    public ResultMsg getInfo(int teacherId) {
        TbTeacher teacher = this.getById(teacherId);

        if (teacher != null) {
            return ResultMsg.success(teacher);
        }
        return ResultMsg.faild(201, "查询失败!");
    }

    @Override
    public ResultMsg teacherLogin(String name, String password, HttpServletRequest request) {
        QueryWrapper<TbTeacher> wrapper = new QueryWrapper<>();
        wrapper.eq("teacher_name", name);
        TbTeacher teacher = this.getOne(wrapper);

        if (teacher == null)
            return ResultMsg.faild(SessionKey.USER_EXITE, "教师不存在");

        boolean e = teacher.getTeacherPass().equals(password);
        //判断密码
        if (e) {
            String jwt = TokenUtils.create(String.valueOf(teacher.getTeacherId()), teacher.getTeacherPass());
            return ResultMsg.success(jwt);
        }

        return null;
    }

    @Override
    public ResultMsg findOne(String name) {

        QueryWrapper<TbTeacher> tbTeacherQueryWrapper = new QueryWrapper<>();
        tbTeacherQueryWrapper.eq("teacher_name", name);

        TbTeacher teacher = this.getOne(tbTeacherQueryWrapper);
        if (teacher != null) {
            return ResultMsg.success(teacher);
        }
        return null;

    }

    @Override
    public ResultMsg Modify(TbTeacher tbTeacher) {
        boolean b = this.saveOrUpdate(tbTeacher);

        if (b=true){
            return ResultMsg.success();
        }else {
            return ResultMsg.faild(203,"操作失败!");
        }
    }

    @Override
    public ResultMsg uploadImg(MultipartFile file) {
        try {
            String img = qiNiuUpload.upload(file);
            return ResultMsg.success(img);
        } catch (Exception e) {
            e.printStackTrace();
        }
       return ResultMsg.faild(201,"上传失败!");
    }


}